package UIFree.graphicTools.angle
{
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class uif_directView extends uif_ecomponent {
		private var _directPos: Point;
		public function uif_directView(parent:uif_component, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, styleClass:Object='') {
			super(parent, ax, ay, awidth, aheight, styleClass);
		}
		
		override protected function updateBounds():uif_component {
			if (!_directPos) {
				var inner: Rectangle = getInner();
				_directPos = new Point(inner.x + inner.width / 2, inner.y);
			}
			return super.updateBounds();
		}
		
		override protected function paint(a_styleClass:String, a_method:String='draw_direct'):void {
			super.paint(a_styleClass, a_method);
		}
		
		public function get directPos(): Point {
			return _directPos; 
		}
		
		public function set directPos(a_directPos: Point): void {
			_directPos = a_directPos;
			repaint();
			dispatchEvent(new Event(Event.CHANGE));
		}
		
		public function get angle(): Number {
			var c: Point = _directPos.subtract(center);
			var angle: Number = (Math.atan2(c.x, -c.y) + Math.PI * 2) % (Math.PI * 2);
			return  angle/Math.PI * 180;
		}
		
		public function get length(): Number {
			var c: Point = center;
			var l: Number = c.subtract(directPos).length / c.x;
			return l>1?1:l;
		}
		
		public function set angle(a_angle: Number): void {
			directPos = center.add(Point.polar(length, a_angle/180 * Math.PI));
		}
		
		public function set length(a_length: Number): void {
			var c: Point = center;
			directPos = center.add(Point.polar(a_length * c.x, angle));
		}
		
		public function draw_direct(x: int, y: int, width: int, height: int, style: Object): void {
			begin2(style);
			var c		: Point = center;
			var radius	: Number = width * 0.04;
			graphics.moveTo(c.x, c.y);
			graphics.lineTo(_directPos.x, _directPos.y);
			if (style.fill_color) this.graphics.endFill();
			
			begin2(style);
			graphics.drawCircle(c.x, c.y, radius * 1.5);
			graphics.drawCircle(_directPos.x, _directPos.y, radius);
			if (style.fill_color) this.graphics.endFill();			
		}
	}
}